package Main;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import LangDet.LangDetect;
import Youtube.actions.YoutubeData;
import Youtube.authentication.Authentication;

import com.cybozu.labs.langdetect.LangDetectException;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Subscription;
import com.google.api.services.youtube.model.SubscriptionListResponse;
import com.google.common.collect.Lists;
import com.google.gdata.client.youtube.YouTubeService;

/**
 * Authorizes user, runs Youtube to get video recommandation.
 *
 * @param args command line args (not used).
 */

public class Main {

	public static Map<String, Double> video2Activity = new HashMap<String, Double>();
	private static final int NUM_VIDEO = 25;

	public static void main(String[] args) {

		// Scope required to upload to YouTube.
		List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube", "https://gdata.youtube.com");
		Map<String, Double> video2Sentiment = new HashMap<String, Double>();
		Map<Subscription, Integer> mapSubscription = new HashMap<Subscription, Integer>();
		Map<String, Double> finalMapVideo = new HashMap<String, Double>();
		Authentication auth = new Authentication();
		YoutubeData youData = new YoutubeData();
		LangDetect lg = new LangDetect();
		List<String> ranking = new ArrayList<String>();

		int levelDepth = 3;
		int levelStart = 2;

		try {
			lg.init("/Users/alessio/Desktop/SII-library/langdetect-03-03-2014/profiles/");
		} catch (LangDetectException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}


		try {
			// Authorization.
			Credential credential = auth.authorize(scopes);	
			credential.refreshToken();
			// YouTube and YouTubeService objects used to make all API requests for API 3.0.
			YouTube youtube = new YouTube.Builder(Authentication.HTTP_TRANSPORT, Authentication.JSON_FACTORY, credential)
			.setApplicationName("youtube-Recommender").build();

			//To use API 2.0
			YouTubeService service = new YouTubeService("youtube-Recommender");	
			
			//My list of video liked
			List<String> myVideosLike = youData.getMyVideoLike(youtube);

			/*
			 * Now that the user is authenticated, the app makes a subscription list request to get the
			 * authenticated user's subscriptions. 
			 */
			YouTube.Subscriptions.List subscriptionRequest = youtube.subscriptions().list("snippet");
			subscriptionRequest.setMine(true).setMaxResults(50L);

			SubscriptionListResponse subscriptionResult = subscriptionRequest.execute();

			// Gets the list of subscriptions associated with the user.
			List<Subscription> subscriptionList = subscriptionResult.getItems();
			for(Subscription s :subscriptionList ){
				mapSubscription.put(s,1);
			}
			mapSubscription = youData.subLevelSubscription(youtube, mapSubscription, subscriptionList, levelStart , levelDepth );
			//System.out.println(mapSubscription);
			video2Activity = youData.getVideo2Activity(mapSubscription, youtube);
			//System.out.println(video2Activity);
			finalMapVideo = youData.filterVideoLike(video2Activity, myVideosLike);
			//System.out.println(finalMapVideo);
			for (String id : finalMapVideo.keySet()) {
				double s = youData.videoSentiment(id, service, lg);
				if (s!=-2)
					video2Sentiment.put(id,s);
			}

			// Show the first NUM_VIDEO videos
			ranking = youData.videoList(video2Activity, video2Sentiment).subList(0, NUM_VIDEO);
			String home = System.getProperty("user.home");
			File f = new File("Video_response.txt");
			FileOutputStream fos = new FileOutputStream(home+"/"+f);
			OutputStreamWriter out = new OutputStreamWriter(fos);
			for (String url : ranking) {
				out.write(url+"\n"+"\n");
			}
			out.close();
			fos.close();

		} catch (GoogleJsonResponseException e) {
			e.printStackTrace();
			System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
					+ e.getDetails().getMessage());

		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

}
